package com.example.studentdemo.service.impl;

import com.example.studentdemo.dto.NotificationDTO;
import com.example.studentdemo.entity.Notification;
import com.example.studentdemo.service.NotificationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NotificationServiceImpl implements NotificationService {

    // 在实际项目中，这里应该使用数据库存储
    // 现在我们使用内存存储来演示功能
    private final List<Notification> notifications = new ArrayList<>();
    private Long idCounter = 1L;

    @Override
    public Notification saveNotification(NotificationDTO notificationDTO) {

        Notification notification = new Notification(
            notificationDTO.getTitle(),
            notificationDTO.getContent(),
            notificationDTO.getRecipientType(),
            notificationDTO.getRecipientId()
        );
        notification.setId(idCounter++);
        notifications.add(notification);
        return notification;
    }

    @Override
    public List<Notification> getNotificationsByUser(Long userId, String userType) {
        return notifications.stream()
            .filter(notification -> {
                // 如果通知是发给所有人的
                if ("ALL".equals(notification.getRecipientType())) {
                    return true;
                }
                // 如果通知是发给特定类型的用户
                if (userType.equals(notification.getRecipientType())) {
                    // 如果没有指定特定用户，则返回
                    if (notification.getRecipientId() == null) {
                        return true;
                    }
                    // 如果指定了特定用户，则需要匹配用户ID
                    return userId.equals(notification.getRecipientId());
                }
                // 如果是指定给特定用户的
                if (notification.getRecipientId() != null) {
                    return userId.equals(notification.getRecipientId());
                }
                return false;
            })
            .collect(Collectors.toList());
    }

    @Override
    public boolean markAsRead(Long notificationId, Long userId) {
        return notifications.stream()
            .filter(notification -> notification.getId().equals(notificationId))
            .findFirst()
            .map(notification -> {
                notification.setIsRead(true);
                return true;
            })
            .orElse(false);
    }

    @Override
    public Long getUnreadCount(Long userId, String userType) {
        return notifications.stream()
            .filter(notification -> !notification.getIsRead())
            .filter(notification -> {
                // 如果通知是发给所有人的
                if ("ALL".equals(notification.getRecipientType())) {
                    return true;
                }
                // 如果通知是发给特定类型的用户
                if (userType.equals(notification.getRecipientType())) {
                    // 如果没有指定特定用户，则返回
                    if (notification.getRecipientId() == null) {
                        return true;
                    }
                    // 如果指定了特定用户，则需要匹配用户ID
                    return userId.equals(notification.getRecipientId());
                }
                // 如果是指定给特定用户的
                if (notification.getRecipientId() != null) {
                    return userId.equals(notification.getRecipientId());
                }
                return false;
            })
            .count();
    }
}